/*
 * @Project Name: demo
 * @File Name: BeanConfiguration
 * @Package Name: com.sayimo.cn.test.demo.config
 * @Date: 2018/4/22 19:07
 * @Creator: linshixing-1199
 * @line------------------------------
 * @修改人:
 * @修改时间:
 * @修改内容:
 */

package com.sm.shoplook.config;

import com.sm.shoplook.interceptor.SensitivityFilter;
import com.sm.shoplook.util.CodecUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;

import javax.servlet.DispatcherType;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author chenpy-1072
 * @desc BeanConfiguration
 * @date 2018/4/22 19:07
 * @see
 */
@Configuration
public class BeanConfiguration {

	@Autowired
	private ThymeleafViewResolver thymeleafViewResolver;

	@Bean
	public ServletContextInitializer servletContextInitializer() {
		// 测试提交 aaa
		return new ServletContextInitializer() {

			@Override
			public void onStartup(ServletContext servletContext) throws ServletException {
				Map<String, Object> vars = new HashMap<>();
				vars.put("source_version", CodecUtil.encryptMD5(String.valueOf((new Date()).getTime() + "_VERSION")));
				thymeleafViewResolver.setStaticVariables(vars);
			}
		};
	}

	@Bean
	public FilterRegistrationBean shiroFilterRegistrationBean() {
		FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
		DelegatingFilterProxy proxy = new DelegatingFilterProxy();
		proxy.setTargetFilterLifecycle(true);
		proxy.setTargetBeanName("shiroFilter");
		filterRegistrationBean.setFilter(proxy);
		filterRegistrationBean.setDispatcherTypes(DispatcherType.ASYNC, DispatcherType.ERROR, DispatcherType.FORWARD);
		return filterRegistrationBean;
	}

    @Bean
    public FilterRegistrationBean sensitivityFilter() {
	    FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        SensitivityFilter sensitivityFilter = new SensitivityFilter();
        filterRegistrationBean.setFilter(sensitivityFilter);
        filterRegistrationBean.addUrlPatterns("/front/*");
        return filterRegistrationBean;
    }
}
